package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.ProtectAddressListAdapter;
import com.szgr.baoyin.model.ProtectAddress;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProtectAddressActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private List<ProtectAddress> data;
    private ProtectAddressListAdapter listAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_protect_address);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("维保地址");
    }

    @Override
    public void initView() {
        findViewById(R.id.address_add).setOnClickListener(this);
        data = new ArrayList<>();
        ListView listView = (ListView) findViewById(R.id.address_list_view);
        listAdapter = new ProtectAddressListAdapter(this, data);
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddress();
    }

    private void getAddress() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_PROTECT_ADDRESS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("---get address--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            JSONArray address_list = datas.getJSONArray("address_list");
                            Gson gson = new Gson();
                            data = gson.fromJson(address_list.toString(),new TypeToken<List<ProtectAddress>>(){}.getType());
                            if(data.size() == 0){
                                ToastUtils.showToast("还没有添加任何地址");
                            }else{
                                listAdapter.setData(data);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key", MyApplication.getmInstance().getKey());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.address_add:
                startActivity(new Intent(this,AddProtectAddressActivity.class));
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ProtectAddress protectAddress = data.get(position);
        Intent intent = new Intent();
        intent.putExtra("address",protectAddress);
        setResult(209,intent);
        finish();
    }

}
